package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.mapper.TbSkuMapper;
import com.heima.pojo.TbSku;
import com.heima.pojo.dto.SkuDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.SkuVo;
import com.heima.service.ITbSkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author nan
 * @since 2024-06-06
 */
@Service
public class TbSkuServiceImpl extends ServiceImpl<TbSkuMapper, TbSku> implements ITbSkuService {

    @Autowired
    private TbSkuMapper tbSkuMapper;

    /**
     * 分页查询商品信息
     *
     * @param pageIndex
     * @param pageSize
     * @param skuName
     * @return
     */
    @Override
    public PageResponse<SkuVo> getByPage(Integer pageIndex, Integer pageSize, String skuName) {
        Page<SkuVo> page = new Page<>(pageIndex, pageSize);
        Page<SkuVo> skuPage = tbSkuMapper.getByPage(page, skuName);
        PageResponse<SkuVo> pageResponse = new PageResponse<>();
        pageResponse.setPageIndex(skuPage.getCurrent());
        pageResponse.setPageSize(skuPage.getSize());
        pageResponse.setTotalPage(skuPage.getPages());
        pageResponse.setTotalCount(skuPage.getTotal());
        pageResponse.setCurrentPageRecords(List.copyOf(skuPage.getRecords()));
        return pageResponse;
    }

    @Override
    public Boolean updateById(String skuId, SkuDto skuDto) {
        TbSku tbSku = BeanUtil.toBean(skuDto, TbSku.class);
        tbSku.setSkuId(Long.parseLong(skuId));
        return updateById(tbSku);
    }
}
